package com.example.demo.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.util.StringUtils;

/**
 * @ClassName: PhoneCode
 * TODO:短信验证码工具类
 * @Author: zjf
 * @CreateDate: 2020-11-05 14:51
 * @Version: 0.0.1
 */
public class PhoneCode {

    private static String code;


    /**
     * @Description:阿里云短信服务配置
     * @Author: zjf
     * @Date: 2020/11/5
     * @Param mobile
     * @return
     **/
    public static String getPhoneMsg(String mobile){

        System.out.println(mobile);
        if (StringUtils.isEmpty(mobile)){
            System.out.println("手机号码为空");
            return "null";
        }

        // 设置超时时间-可自行调整
        System.setProperty(StaticPeram.defaultConnectTimeout,StaticPeram.Timeout);
        System.setProperty(StaticPeram.defaultReadTimeout,StaticPeram.Timeout);

        //初始化ascClient需要的几个参数
        final String product = StaticPeram.product;// 短信API产品名称（短信产品名固定，无需修改）
        final String domain = StaticPeram.domain;// 短信API产品域名（接口地址固定，无需修改）

        //替换成你的AK
        final String accessKeyId = StaticPeram.accessKeyId;
        final String accessKeySecret = StaticPeram.accessKeySecret;

        //初始化ascClient,暂时不支持多region
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(StaticPeram.domain);
        request.setSysVersion(StaticPeram.version);
        request.setSysAction(StaticPeram.product);
        request.putQueryParameter("RegionId","cn-hangzhou");
        request.putQueryParameter("PhoneNumbers",mobile);
        request.putQueryParameter("SignName",StaticPeram.SignName);
        request.putQueryParameter("TemplateCode",StaticPeram.TemplateCode);
        request.putQueryParameter("TemplateParam",vcode());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        }catch (ServerException se){
            se.printStackTrace();
            return "由于系统维护，暂时无法注册！！！";
        }catch (ClientException ce){
            ce.printStackTrace();
            return "由于系统维护，暂时无法注册！！！";
        }

        return "true";
    }


    /**
     * 生成6位随机验证码
     * @return
     */
    private static String vcode(){
        String vcode = "";
        for (int i=0;i<6;i++){
            vcode = vcode + (int)(Math.random() * 9);
        }
        String code="{\"code\":"+vcode+"}";
        System.out.println(code);
        return code;
    }


    public static void main(String[] args) {
        String xyz = PhoneCode.getPhoneMsg("18294018665");
        System.out.println(xyz);
    }
}
